package web_wms_mapper

import (
	"context"
	"gorm.io/gorm"
	"transport/web/web_wms_model"
)

type CustomerMapper struct {
	DB  *gorm.DB
	Ctx context.Context
}

func (mapper *CustomerMapper) Create(m *web_wms_model.WmCustomer) (ret *web_wms_model.WmCustomer, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CustomerMapper) Update(m *web_wms_model.WmCustomer) (ret *web_wms_model.WmCustomer, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmCustomer{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CustomerMapper) CreateOrUpdate(m *web_wms_model.WmCustomer) (ret *web_wms_model.WmCustomer, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *CustomerMapper) QueryById(id string) (m *web_wms_model.WmCustomer, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmCustomer{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *CustomerMapper) QueryRouteLabelList() (m []string, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Table("wm_customer wc").Select("wc.route").Where("wc.customer_type = ? and wc.route != ? and wc.route != '' and wc.route is not null", "CO", "*").Group("wc.route").Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *CustomerMapper) QueryByIds(ids []string) (m []*web_wms_model.WmCustomer, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmCustomer{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *CustomerMapper) DeleteById(id string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_wms_model.WmCustomer{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *CustomerMapper) DeleteByIds(ids []string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_wms_model.WmCustomer{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *CustomerMapper) Page(pageIndex, pageSize int) (items []*web_wms_model.WmCustomer, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *CustomerMapper) List(customerType string) (items []*web_wms_model.WmCustomer, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if customerType != "" {
		table = table.Where("customer_type = ? or customer_type= ?", customerType, "VE")
	}
	if err := table.Find(&items).Group("id").Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
